package com.lulee007;


import java.io.*;

public class Main {

    public static void main(String[] args) {

        String sourceFolder = null;
        if (args == null) {
            sourceFolder = "/path to your image folder/";
            convert(sourceFolder);
        } else if (args.length == 0) {
            System.out.println("请设置路径：如（-c:\\image\\）");
        } else {
            if (args.length == 1) {
                convert(args[0]);
            } else {
                convert(args[0], args[1]);
            }
        }

    }

    private static void convert(String sourceFolder) {
        convert(sourceFolder, null);
    }

    /**
     * 转换png
     * @param sourceFolder 源目录
     * @param desFolder 保存目录，如果null，则覆盖源文件
     */
    private static void convert(String sourceFolder, String desFolder) {
        String cmd;
        if (sourceFolder.trim().equalsIgnoreCase("")) {
            System.out.println("目录不能为空");
            return;
        }

        File checkFile = new File(sourceFolder);
        if (!checkFile.exists() || !checkFile.isDirectory()) {
            System.out.println("源目录不正确：" + sourceFolder);
            return;
        }
        if (desFolder != null) {
            checkFile = new File(desFolder);
            if (!checkFile.exists() || !checkFile.isDirectory()) {
                System.out.println("保存目录不正确：" + desFolder);
                return;
            }
        }
        try {
            Runtime run = Runtime.getRuntime();
            Search search = new Search();
            search.dosearch(sourceFolder, ".*\\.png");
            for (File f : search.list) {
                cmd = "convert " + f.getAbsolutePath() + " -strip " + (desFolder == null ? f.getAbsolutePath() : desFolder + System.getProperty("file.separator") + f.getName());
                System.out.println(cmd);
                Process p = run.exec(cmd);// 启动另一个进程来执行命令
                BufferedInputStream in = new BufferedInputStream(p.getInputStream());
                BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
                String lineStr;
                while ((lineStr = inBr.readLine()) != null)
                    //获得命令执行后在控制台的输出信息
                    System.out.println(lineStr);// 打印输出信息
                //检查命令是否执行失败。
                if (p.waitFor() != 0) {
                    if (p.exitValue() != 0) {//p.exitValue()==0表示正常结束，1：非正常结束
                        System.err.println("命令执行失败!");
                        // get the error stream of the process and print it
                        InputStream error = p.getErrorStream();
                        StringBuffer out = new StringBuffer();
                        byte[] b = new byte[error.available() + 1];
                        for (int n; (n = error.read(b)) != -1; ) {
                            out.append(new String(b, 0, n));
                        }
                        error.close();
                        System.out.println(out.toString());
                    }
                }
                inBr.close();
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
